/*-------------------------------------------------------------------------------
* file:     app_parameter.c
* brief:    application
* copyright Copyright (C) 2020-2022 @ NanoIC
* Date:     2023-09-06
* Version:  V1.00
* Author:   
*-------------------------------------------------------------------------------*/

/* include ----------------------------------------------------------------*/
#include "app_parameter.h"

/* externs function--------------------------------------------------------*/

/* externs variablesr------------------------------------------------------*/
extern OneWireSerialNumber one_wire_app_serial;
extern OneWireSetParaItemData one_wire_pata_item;
extern uint8_t one_wire_bus_app_sleep_or_wakeup_flag;

extern uint8_t one_wire_temp_ack_buf[ONE_WIRE_BUS_TEMP_ACK_BUF_LEN];
extern uint8_t one_wire_temp_ack_len;

uint8_t pad_cover_state = 0;
uint8_t pad_lcd_state = 1;
uint8_t kb_lock_state = 1;
uint8_t kb_lock_state_flag = 0;

uint8_t get_kb_lock_flag = 0;
uint8_t get_kb_lock_cnt = 0;
// extern volatile bool global_keyboard_stop_scan_flag;
/* Private variablesr------------------------------------------------------*/

/* Private function--------------------------------------------------------*/

/**
 * @brief 对单总线收到参数设置命令进行处理
 * 
 * @param p_rec_packet 收到数据包指针
 * 
 * @return true : 成功
 * @return false : 失败
 */

					
						
bool one_wrie_bus_app_rec_parameter_set(SourcePacketStr *p_rec_packet)
{
    bool ret;
    one_wire_temp_ack_len = 0x00;
    ret = false;
	
		
 
//	uint8_t data_buffer[21] = {0x5A, 0x5A, 0x5A, 0x5A, 0x0D, 0x00, 0x80, 0x00,0xFF,0xFF,0x00,0x00,0x3A,0x0D,0x01,0x01,0x01,0xAA,0xBD,0x0A,0x0D};
    #if 0
        printf("recdata  =  ");
        console_log_num_byte(&p_rec_packet->data[0],p_rec_packet->len);
    #endif
	
	switch(p_rec_packet->cid)
	{
		case 0x06:	//lock
			if(p_rec_packet->data[2]==0x01)
			{
				kb_lock_state = 1;
			}
			else if(p_rec_packet->data[2]==0x00){
				kb_lock_state = 0;
			}
			rec_single_evt_test(MaintenanceServiceID,REMOTE_KB_LOCK_STA,0x01,0x01,p_rec_packet->data[2]);//bonding_success
			if(get_kb_lock_flag==0 || get_kb_lock_flag==1)
			{
				get_kb_lock_flag=2;
			}
			get_kb_lock_cnt=0;
		break ;
		case 0x09:
			//关灯
		if (p_rec_packet->data[0] == 0x01 && p_rec_packet->data[1] == 0x01) 
		{
			keyboard_led_status = p_rec_packet->data[2];
			app_led_ctrl(keyboard_led_status);
		}
		break;
		case 0x0A:	//开合盖
			if(p_rec_packet->data[2]==0x01)
			{
				pad_cover_state = 1;
			}
			else if(p_rec_packet->data[2]==0x00){
				pad_cover_state = 0;
			}
			rec_single_evt_test(MaintenanceServiceID,REMOTE_PAD_COVER_STA,0x01,0x01,p_rec_packet->data[2]);//bonding_success
		break ;
		
		case 0x0B:	//亮灭屏
			if(p_rec_packet->data[2]==0x01)
			{
				pad_lcd_state = 1;
			}
			else if(p_rec_packet->data[2]==0x00){
				pad_lcd_state = 0;
			}
			rec_single_evt_test(MaintenanceServiceID,REMOTE_PAD_LCD_STA,0x01,0x01,p_rec_packet->data[2]);//bonding_success
			
		break ;
		
		case 0x0C:	//时间日期
			rec_single_evt_test(MaintenanceServiceID,REMOTE_PAD_TIME,p_rec_packet->data[2],p_rec_packet->data[3],p_rec_packet->data[4]);
			rec_single_evt_test(MaintenanceServiceID,REMOTE_PAD_TIME,p_rec_packet->data[5],p_rec_packet->data[6],p_rec_packet->data[7]);
		break ;
		
		case 0x0D:
		if(p_rec_packet->data[2]==0x02)
		{
			if(!device_info_send_flag )  //发送设备描述符
			{
				handshake=2;
				
//				printf("handshake %d\r\n",handshake);
			   device_info_send_flag=1;
			}
		}
		else if(p_rec_packet->data[2]==0x03){
			handshake=3;
//			device_version_info_send_flag=1;
//			global_key_power_on_flag=1;
		}
		rec_single_evt_test(MaintenanceServiceID,REMOTE_EVT_PAIRING,0x01,0x01,p_rec_packet->data[2]);//bonding_success
		break ;

		case 0x0E:
		
			device_extend_info_send_flag=1;
			syn_100us_timer_tick=0;				
		
		break ;
		case 0x15:
			if(global_key_power_on_flag)
			{
				if(p_rec_packet->data[2]==0x01)
				{
					app_factory_test_sleep_cmd_set();
				}
				else if(p_rec_packet->data[2]==0x00){
					one_wire_bus_app_sleep_or_wakeup_flag = 0x00;
				}
			}

			rec_single_evt_test(MaintenanceServiceID,REMOTE_MCU_STATUS,0x01,0x01,p_rec_packet->data[2]);//bonding_success
			
		break ;
		
		case 0xff:
			if(p_rec_packet->data[0] == 0x01 && p_rec_packet->data[1] == 0x01&&p_rec_packet->data[2] == 0x03)
			{
				receive_pack_num++;
				printf("receive_pack_num=%d",receive_pack_num);
			}
			
		break;
	}

}
/**
 * @brief 对单总线收到状态读取命令进行处理
 * 
 * @param p_rec_packet 收到数据包指针
 * 
 * @return true : 成功
 * @return false : 失败
 */
bool one_wrie_bus_app_rec_status_read(SourcePacketStr *p_rec_packet)
{
    bool ret;
    uint8_t i;

    ret = false;
	
	switch(p_rec_packet->cid)
	{
		case 0x07:

			device_version_info_send_flag=1;
			syn_100us_timer_tick=0;			
	//		ota_state_flag=1;
		break ;
 
	}
    ret = true;
    return ret;
}


